<template>
	<Layout class="p-5 bg-white">
		<Form :label-width="100">
			<Row class="p-1">
				<Col span="12">
					<FormItem class="" label="广告图片:">
						<div class="d-flex flex-row">
							<label class="ivu-input">{{coverFile.name}}</label>
							<label style="line-height: 30px;" class="ivu-btn ivu-btn-default ml-1" for="cover">选择图片</label>
							<input @change="fileChange($event,coverFile)" id="cover" style="display: none;" type="file"></input>
						</div>
						<div class="d-flex flex-row mt-2">
							<img v-if="coverFile.url != ''" class="w-50" :src="coverFile.url" />
						</div>
					</FormItem>
					<FormItem class="" label="广告状态">
						<Select v-model="adv.statu" clearable >
							<Option value="1">激活</Option>
							<Option value="0">禁用</Option>
						</Select>
					</FormItem>
				</Col>
			</Row>
			<Row class="p-1">
				<Col span="12">
					<FormItem class="text-right">
						<Button @click="submit" type="primary">提交</Button>
						<Button @click="cancel" class="ml-2">取消</Button>
					</FormItem>
				</Col>
			</Row>
			
		</Form>
	</Layout>
</template>

<script>
	export default {
		data() {
			return {
				adv: {
					statu: "1",
				},
				coverFile: {
					name: "",
					file: undefined,
					url: "",
				},
				url:"",
			}
		},
		methods: {
			fileChange: function(e,imgItem) {
				imgItem.file = e.target.files[0];
				imgItem.name = imgItem.file.name;
				
				let reader = new FileReader();
				reader.onloadend = function() {
					imgItem.url = this.result;
				}
				reader.readAsDataURL(imgItem.file);
				imgItem.onProgress = this.onProgress;
			},
			onProgress: function(pData) {
				console.log(pData);
			},
			
			submit: function() {
				let self = this;
				

				if (this.common.isEmptyString(this.coverFile.name)) {
					this.message.warning("请选择广告图片文件");
					return;
				}
				this.common.submitFile(this.coverFile).then(function(res) {
					self.url = res.Location;
					self.http.post({
						url: "/adv/new",
						param: {
							statu:self.adv.statu,
							url:self.url,
						}
					}).then(data => {
						self.message.info("添加成功");
						self.$tabs.close();
					});
				}).catch(function(err) {
					this.message.error("广告图片上传失败");
				});
				
			},
			cancel: function() {
				this.$tabs.close();
			},
			init: function() {	
			}
		},
		created: function(){
			this.init();
		},
	}
</script>

<style>
</style>
